package com.kanshu.ksgb.fastread.doudou.module.reader.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.presenter.BookPresenter;

import java.util.List;

/**
 * 阅读器更多popwindow
 */
public class ReaderPopupWindowHelper {

    public static void showPopupWindow(Context context, View showView, Toolbar mToolbar, final String bookId) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.popupwindow_reader_more_layout, null);
        PopupWindow popupWindow = new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setAnimationStyle(R.style.popupwindow_anim_style);
        TextView tv = view.findViewById(R.id.add_to_shelf);
        boolean shelfExist = false;
        List<BookInfo> bookInfos = SettingManager.getInstance().getShelfInfos();
        if (Utils.isEmptyList(bookInfos)) {
            bookInfos = SettingManager.getInstance().getShelfInfosFromSd();
        }
        if (!Utils.isEmptyList(bookInfos)) {
            for (BookInfo bookInfo : bookInfos) {
                if (TextUtils.equals(bookId, bookInfo.book_id)) {
                    shelfExist = true;
                    tv.setText("已加入书架");
                    break;
                }
            }
        }
        final boolean flg = shelfExist;
        // 获取到最精确的显示位置
        popupWindow.showAsDropDown(showView, -DisplayUtils.dip2px(context, flg? 88:83), mToolbar == null ? 0 : (mToolbar.getHeight() - showView.getHeight()) / 2);
        view.findViewById(R.id.add_to_shelf).setOnClickListener(v -> {
            if (flg) {
                ToastUtil.showStaticMessage("已经加入书架");
                popupWindow.dismiss();
                return;
            }
            BookPresenter presenter = new BookPresenter(null);
            presenter.joinBookShelf(bookId);
            popupWindow.dismiss();
        });
        view.findViewById(R.id.book_details).setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", bookId);
            popupWindow.dismiss();
        });
    }
}
